<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

        <el-form-item label="库位名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入库位名称" clearable size="small"
            @keyup.enter.native="handleQuery" />
        </el-form-item>


        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
            <el-option v-for="dict in dict.type.wms_locator_status" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>

        <!-- <el-form-item label="删除标识" prop="delFlag">
      <el-select v-model="queryParams.delFlag" placeholder="请选择删除标识" clearable size="small">
        <el-option
          v-for="dict in dict.type.sys_del_flag"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
        />
      </el-select>
    </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['wms:locator:add']">新增</el-button>
          <el-button type="warning" plain icon="el-icon-plus" size="mini" @click="handleBatchAdd" v-hasPermi="['wms:locator:add']" v-if="cabinetId">批量新增</el-button>
          <el-button type="warning"  plain icon="el-icon-edit" size="mini" @click="openBatchPlan" v-hasPermi="['wms:locator:edit']" v-if="cabinetGroupId  && !isPlan">批量分配</el-button>
          <template v-if="isPlan">
            <el-button type="primary"   size="mini" @click="handleBatchUpdate" v-hasPermi="['wms:locator:edit']" >确认</el-button>
            <el-button size="mini" @click="closeBatchPlan" v-hasPermi="['wms:locator:edit']" >取消</el-button>
          </template>         <!-- <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['wms:locator:edit']">修改</el-button>
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['wms:locator:remove']">删除</el-button> -->
        </el-form-item>
      </el-form>

      <!-- <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['wms:locator:add']">新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
            v-hasPermi="['wms:locator:edit']">修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
            v-hasPermi="['wms:locator:remove']">删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['wms:locator:export']">导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row> -->
      <el-descriptions class="margin-top" :column="4" border v-if="cabinetGroupId">
        <el-descriptions-item>
          <template slot="label">
            编号
          </template>
          {{groupInfo.cno}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            名称
          </template>
          {{groupInfo.name}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            所在库区
          </template>
          {{groupInfo.areaName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            所在仓库
          </template>
          {{groupInfo.storeName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            架子数
          </template>
          {{groupInfo.cnums}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            格口数
          </template>
          {{groupInfo.lnums}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            物品数
          </template>
          {{groupInfo.totals}}
        </el-descriptions-item>
      </el-descriptions>
      <el-descriptions class="margin-top" :column="4" border v-if="cabinetId">
        <el-descriptions-item>
          <template slot="label">
            编号
          </template>
          {{cabinetInfo.cno}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            名称
          </template>
          {{cabinetInfo.name}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            所在货柜
          </template>
          {{cabinetInfo.areaName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            所在库区
          </template>
          {{cabinetInfo.areaName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            所在仓库
          </template>
          {{cabinetInfo.storeName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            层数
          </template>
          {{cabinetInfo.floors}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            格口数
          </template>
          {{cabinetInfo.lnums}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template slot="label">
            物品数
          </template>
          {{cabinetInfo.totals}}
        </el-descriptions-item>
      </el-descriptions>
      <!-- <el-table v-loading="loading" :data="locatorList" @selection-change="handleSelectionChange" border stripe>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="仓库" align="center" prop="storeName" />
        <el-table-column label="库区" align="center" prop="areaName" />
        <el-table-column label="货柜" align="center" prop="groupName" />
        <el-table-column label="架子" align="center" prop="cabinetName" />
        <el-table-column label="库位" align="center" prop="name" />
        <el-table-column label="财物数量" align="center" prop="pnums"  >
              <template slot-scope="scope">
                <el-link type="primary" @click="handleProperty(scope.row)">{{scope.row.pnums}}[查看]</el-link>
              </template>
            </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.wms_locator_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['wms:locator:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['wms:locator:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table> -->
      <el-row v-loading="loading" class="mt10" :gutter="5">
        <el-col :span="6" v-for="item in locatorList" class="mb5">
          <el-card class="box-card" shadow="hover">
            <div slot="header" class="clearfix">
              <el-tag type="info" v-if="item.status == 0">{{(item.cabinetName?item.cabinetName:'')+item.name}}</el-tag>
              <el-tag type="primary" v-if="item.status == 1">{{(item.cabinetName?item.cabinetName:'')+item.name}}</el-tag>
              <el-tag type="warning" v-if="item.status == 2">{{(item.cabinetName?item.cabinetName:'')+item.name}}</el-tag>
              <el-tag v-if="item.status != 0" class="ml10" @click="handleDrawShow(item)"><i class="el-icon-view"></i> {{item.totals}}</el-tag>
              <el-button class="fr" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(item)" v-hasPermi="['wms:locator:remove']"></el-button>
              <el-button class="fr" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(item)" v-hasPermi="['wms:locator:edit']"></el-button>
            </div>
            <div class="text item" style="height: 180px;overflow-y:auto;">
              <!-- <template v-if="item.status == 0"><el-tag type="info">空闲</el-tag></template> -->
              <!-- <template v-else v-for="property in item.propertyVos">
                <el-badge :value="property.nums" class="mr20 mb20">
                  <el-tag >{{property.name}}</el-tag>
                </el-badge>
              </template> -->
              <el-table :data="item.propertyVos" max-height="180" empty-text="空闲" :show-header="false">
                  <el-table-column fixed label="物品" prop="name"></el-table-column>
                  <el-table-column prop="nums" label="数量" width="60"></el-table-column>
                </el-table>
            </div>
            <el-checkbox v-if="item.isPlan" class="fr mb10" @change="checkboxChange($event,item.id)"></el-checkbox>
          </el-card>
        </el-col>
      </el-row>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    <!-- 添加或修改库位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="480px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">

        <el-form-item label="所属仓库" prop="storeId">
          <el-select v-model="form.storeId" placeholder="请选择所属仓库" style="width: 100%;" >
            <el-option v-for="item in storeOptions" :key="item.id" :label="item.label" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属库区" prop="areaId">
          <el-select v-model="form.areaId" placeholder="请选择所属库区" style="width: 100%;">
            <el-option v-for="item in areaOptions" :key="item.id" :label="item.label" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属货柜" prop="cabinetGroupId">
          <el-select v-model="form.cabinetGroupId" placeholder="请选择所属货柜" style="width: 100%;" >
            <el-option v-for="item in cabinetGroupOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属货架" prop="cabinetId" v-if="form.isShow">
          <el-select v-model="form.cabinetId" placeholder="请选择所属货架" style="width: 100%;">
            <el-option v-for="item in cabinetOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="托盘编号" prop="floors" v-if="form.areaNo == 'ZX'">
          <el-input v-model="form.floors" placeholder="请输入托盘编号" @change="floorChange"/>
        </el-form-item>
        <template v-if="form.isShow">
          <el-form-item label="第几层" prop="floors">
            <el-input v-model="form.floors" placeholder="请输入所在层" @change="floorChange"/>
          </el-form-item>
          <el-form-item label="第几箱" prop="boxs">
            <el-input v-model="form.boxs" placeholder="请输入所在箱" @change="boxsChange"/>
          </el-form-item>
        </template>
        <el-form-item label="库位名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入库位名称" />
        </el-form-item>
        <el-form-item label="物品形态" prop="shape" >
           <el-radio-group v-model="form.shape">
             <template v-for="dict in dict.type.wms_property_shape">
                <el-radio :label="dict.value">{{dict.label}}</el-radio>
             </template>
           </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="openBatch" width="480px" append-to-body>
      <el-form ref="batchForm" :model="batchForm" :rules="batchRules" label-width="110px">

        <!-- <el-form-item label="所在区域" prop="cabinetGroupId">
          <el-select v-model="batchForm.cabinetGroupId" placeholder="请选择所属货柜" style="width: 100%;" disabled>
            <el-option v-for="item in cabinetGroupOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item> -->
        <el-form-item label="起始层" prop="startFloor">
          <el-input v-model="batchForm.startFloor" placeholder="请输入起始层 例如:1" />
        </el-form-item>
        <el-form-item label="截止层" prop="endFloor">
          <el-input v-model="batchForm.endFloor" placeholder="请输入截止层 例如:5" />
        </el-form-item>
        <el-form-item label="起始编码" prop="startNo">
          <el-input v-model="batchForm.startNo" placeholder="请输入起始编码 例如:1" />
        </el-form-item>
        <el-form-item label="截止编码" prop="endNo">
          <el-input v-model="batchForm.endNo" placeholder="请输入起始编码 例如:99" />
        </el-form-item>
        <el-form-item label="起始排序" prop="seq">
          <el-input v-model="batchForm.seq" placeholder="请输入起始排序 例如:1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitBatchForm">确 定</el-button>
        <el-button @click="cancelBatch">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="openPlan" width="480px" append-to-body>
      <el-form ref="batchForm"  label-width="110px">
        <el-form-item label="所属货架" prop="cabinetId">
          <el-select v-model="planCabinetId" placeholder="请选择所属货架" style="width: 100%;">
            <el-option v-for="item in cabinetOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitPlanForm">确 定</el-button>
        <el-button @click="cancelPlan">取 消</el-button>
      </div>
    </el-dialog>
    <el-drawer :title="locatorName" :visible.sync="locatorDrawer" :before-close="handleClose" size="75vw">
      <el-card>
        <el-table v-loading="loading" :data="locatorProperties" border stripe height="800">
          <el-table-column type="index" width="55" align="center" fixed="left" />
          <el-table-column label="编号" align="left" prop="pno" width="250" fixed="left"></el-table-column>
          <el-table-column label="名称" align="left" prop="name" min-width="120" fixed="left"/>
          <el-table-column label="所属单位" align="left" prop="deptName"  width="260"/>
          <el-table-column label="分类" align="left" prop="secondClassifyName" width="180"/>
          <el-table-column label="入库数" align="center" prop="status" width="80">
            <template slot-scope="scope">
              <el-badge :value="scope.row.innums" :type="scope.row.innums > 0 ?'success':'info'" class="item"></el-badge>
            </template>
          </el-table-column>
          <el-table-column label="出库数" align="center" prop="status" width="80">
            <template slot-scope="scope">
              <el-badge :value="scope.row.outnums" type="info" class="item"></el-badge>
            </template>
          </el-table-column>
          <el-table-column label="处置数" align="center" prop="status" width="80">
            <template slot-scope="scope">
              <el-badge :value="scope.row.deals" :type="scope.row.deals > 0 ?'danger':'info'" class="item"></el-badge>
            </template>
          </el-table-column>
          <el-table-column label="调用数" align="center" prop="status" width="80">
            <template slot-scope="scope">
              <el-badge :value="scope.row.tnums" :type="scope.row.tnums > 0 ?'warning':'info'" class="item"></el-badge>
            </template>
          </el-table-column>
          <el-table-column label="归还数" align="center" prop="status" width="80">
            <template slot-scope="scope">
              <el-badge :value="scope.row.rnums" :type="scope.row.rnums > 0 ?'success':'info'" class="item"></el-badge>
            </template>
          </el-table-column>
          <el-table-column label="库存数" align="center" prop="status" width="80" fixed="right">
            <template slot-scope="scope">
              <el-badge :value="scope.row.nums" :type="scope.row.nums > 0 ?'primary':'info'" class="item"></el-badge>
            </template>
          </el-table-column>
        </el-table>
        </el-card>
    </el-drawer>
  </div>
</template>

<script>
  import {
    listLocator,
    getLocator,
    delLocator,
    addLocator,
    updateLocator,
    initBean,
    addBatchLocators,
    planBatchLocators
  } from "@/api/wms/locator";
 import { treeselect} from "@/api/system/dept";
 import { storeTreeselect } from "@/api/wms/store";
 import { storeAreasTree } from "@/api/wms/storeArea";
 import { listCabinetGroup,getCabinetGroup} from "@/api/wms/cabinetGroup";
 import { listCabinet,getCabinet} from "@/api/wms/cabinet";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    name: "Locator",
    dicts: ['wms_locator_status', 'sys_del_flag','wms_property_shape'],
    components: {
      Treeselect
    },
    data() {
      return {
        shape:[],
        // 按钮loading
        buttonLoading: false,
        // 遮罩层
        loading: true,
        areaLoading:true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        selected:true,
        // 库位表格数据
        locatorList: [],
       //单位数据
       deptOptions:[],
       //仓库数据
       storeOptions:[],
       //库位数据
       areaOptions:[],
       //智能柜数据
       cabinetGroupOptions:[],
       //柜号数据
       cabinetOptions:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        openBatch:false,
        openPlan:false,
        locatorDrawer: false,
        locatorProperties:[],
        locatorName:'',
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          areaId: undefined,
          storeId:undefined,
          name: undefined,
          status: undefined,
          delFlag: undefined,
          cabinetId:undefined,
          cabinetGroupId:undefined,
          showProperites:1
        },
        defaultProps: {
          children: "children",
          label: "label"
        },
        // 表单参数
        form: {},
        batchForm:{},
        // 表单校验
        rules: {
          storeId: [{
            required: true,
            message: "所属仓库不能为空",
            trigger: "blur"
          }],
          areaId: [{
            required: true,
            message: "所属库区不能为空",
            trigger: "blur"
          }],
          cabinetGroupId: [{
            required: true,
            message: "所属货柜不能为空",
            trigger: "blur"
          }],
          cabinetId: [{
            required: true,
            message: "所在柜号不能为空",
            trigger: "blur"
          }],
          floors: [{
            required: true,
            message: "层数不能为空",
            trigger: "blur"
          }],
          boxs: [{
            required: true,
            message: "箱数不能为空",
            trigger: "blur"
          }],
          name: [{
            required: true,
            message: "库位名称不能为空",
            trigger: "blur"
          }],
        },
        batchRules: {
          cabinetGroupId: [{
            required: true,
            message: "所属货柜不能为空",
            trigger: "blur"
          }],
          startNo: [{
            required: true,
            message: "起始编码不能为空",
            trigger: "blur"
          }],
          endNo: [{
            required: true,
            message: "截止编码不能为空",
            trigger: "blur"
          }],
          startFloor: [{
            required: true,
            message: "起始层不能为空",
            trigger: "blur"
          }],
          endFloor: [{
            required: true,
            message: "截止层不能为空",
            trigger: "blur"
          }],
        },
        selectedArea:{},
        //区分普通柜还是智能柜
        isCabinet:true,
        cabinetGroupId:undefined,
        cabinetId:undefined,
        groupInfo:{},
        cabinetInfo:{},

        locatorIds:[],
        isPlan:false,
        planCabinetId:undefined
      };
    },
    watch: {
      'form.storeId': {
        handler(newVal, oldVal) {
          if(newVal){
            this.getAreaTreeselect(newVal)
          }
        },
        deep: true,
        immediate: true
      },
      'form.areaId': {
        handler(newVal, oldVal) {
          if(newVal){
            this.getCabinetGrouoTreeselect(newVal)
          }
        },
        deep: true,
        immediate: true
      },
      'form.cabinetGroupId': {
        handler(newVal, oldVal) {
          if(newVal){
            this.getCabinet(newVal)
          }
        },
        deep: true,
        immediate: true
      },
    },
    created() {
      const cabinetId = this.$route.query.cabinetId;
      this.queryParams.cabinetId = cabinetId;
      const cabinetGroupId = this.$route.query.cabinetGroupId;
      this.queryParams.cabinetGroupId = cabinetGroupId;
      this.cabinetGroupId = cabinetGroupId;
      this.cabinetId = cabinetId;
      this.getList();
      this.getTreeselect();
      this.getStoreTreeselect();
    },
    methods: {
      /** 查询库位列表 */
      getList() {
        this.loading = true;
        listLocator(this.queryParams).then(response => {
          this.locatorList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
        if(this.cabinetId){
          this.getCabinetInfo()
        }else if(this.cabinetGroupId){
          this.getGroupInfo()
        }
      },
      floorChange(e){
        if(this.form.boxs && e){
          this.form.name="第"+e+"层"+this.form.boxs+"号箱";
        }
      },
      boxsChange(e){
        if(this.form.floors && e){
          this.form.name="第"+this.form.floors+"层"+e+"号箱";
        }
      },
      getInitBean(){
        initBean({cabinetGroupId:this.queryParams.cabinetGroupId,
        cabinetId:this.queryParams.cabinetId}).then(response=>{
            this.form = response.data;
        })
      },
      getGroupInfo(){
        getCabinetGroup(this.cabinetGroupId).then(response => {
          this.groupInfo = response.data;
        });
      },
      getCabinetInfo(){
        getCabinet(this.cabinetId).then(response => {
          this.cabinetInfo = response.data;
        });
      },
     /** 查询部门下拉树结构 */
     getTreeselect() {
       treeselect().then(response => {
         this.deptOptions = response.data;
       });
     },
     /** 查询仓库信息 */
     getStoreTreeselect() {
       storeTreeselect().then(response => {
         this.storeOptions = response.data;
       });
     },
      /** 查询库区信息 */
     getAreaTreeselect(storeId){
       storeAreasTree({storeId:storeId}).then(response=>{
         this.areaOptions = response.data;
       })
     },
      /** 查询智能柜信息 */
     getCabinetGrouoTreeselect(areaId){
       listCabinetGroup({areaId:areaId}).then(response=>{
         this.cabinetGroupOptions = response.rows;
       })
     },
     /** 查询智能柜信息 */
     getCabinet(groupId){
       listCabinet({groupId:groupId}).then(response=>{
         this.cabinetOptions = response.rows;
       })
     },
     /** 判断当前是普通柜还是密码柜 */
     getGroupType(e){
       this.cabinetGroupOptions.forEach(item=>{
         if(item.id == e){
           this.isCabinet = item.type == '1'?true:false
         }
       })
     },

      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.storeId = data.id;
        this.areaName = undefined;
        this.selected = true;
        this.selectedArea={};
        this.getAreaTreeselect();
        this.handleQuery();
      },
      // 节点单击事件
      handleAreaNodeClick(data) {
        this.queryParams.areaId = data.id;
        this.selectedArea = data;
        this.selected = false;
        this.handleQuery();
      },
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children
        };
      },
      handleProperty(row){
        this.$router.push({
          path: '/wms/inventories/list',
          query: {
            pageNum: this.queryParams.pageNum,
            locatorId: row.id,
            status:'0'
          }
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          areaId: undefined,
          name: undefined,
          status: '0',
          createTime: undefined,
          createBy: undefined,
          updateTime: undefined,
          updateBy: undefined,
          delFlag: '0',
          areaName:undefined
        };
        this.resetForm("form");
      },
      // 取消按钮
      cancelBatch() {
        this.openBatch = false;
        this.resetBatch();
      },
      // 取消按钮
      cancelPlan() {
        this.openPlan = false;
        // this.resetBatch();
      },
      resetBatch() {
        this.batchForm = {
          cabinetGroupId: undefined,
          startNo: undefined,
          endNo: undefined,
          seq:undefined,
          startFloor:undefined,
          endFloor:undefined
        };
        this.resetForm("batchForm");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加库位";
        this.getInitBean();
      },
      handleBatchAdd(){
        this.resetBatch();
        this.openBatch = true;
        this.title = "添加库位";
        this.batchForm.areaId = this.selectedArea.id;
        this.batchForm.areaName = this.selectedArea.label;
        this.batchForm.cabinetGroupId = this.cabinetGroupId;
        this.batchForm.cabinetId= this.cabinetId;
        // this.getInitBean();
      },
      openBatchPlan(){
        this.locatorList.forEach(item=>{
          if(!item.cabinetId){
            item.isPlan = true;
          }
        })
        this.isPlan = true;
      },
      closeBatchPlan(){
        this.locatorIds = [];
        this.isPlan = false;
        this.locatorList.forEach(item=>{
          item.isPlan = false;
        })
      },
      handleBatchUpdate(){
        if(this.locatorIds.length == 0){
          this.$modal.msg("请至少选择一项！");
          return false;
        }
        listCabinet({groupId:this.cabinetGroupId}).then(response=>{
          this.cabinetOptions = response.rows;
          this.title = "分配货架";
          this.openPlan = true;
        })
      },
      checkboxChange(flag,val){
        if(flag && !this.locatorIds.includes(val)){
           this.locatorIds.push(val)
        }else{
          if(!flag){
            this.locatorIds = this.locatorIds.filter(locatorId=> locatorId != val);
          }
        }
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.loading = true;
        this.reset();
        const id = row.id || this.ids
        getLocator(id).then(response => {
          this.loading = false;
          this.form = response.data;
          this.open = true;
          this.title = "修改库位";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            if (this.form.id != null) {
              updateLocator(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            } else {
              addLocator(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }).finally(() => {
                this.buttonLoading = false;
              });
            }
          }
        });
      },
      submitBatchForm(){
        this.$refs["batchForm"].validate(valid => {
          if (valid) {
            this.buttonLoading = true;
            addBatchLocators(this.batchForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openBatch = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        });
      },
      submitPlanForm(){
        if(!this.planCabinetId){
          this.$modal.msg("请选择货架！");
          return false;
        }
        planBatchLocators({locatorIds:this.locatorIds,cabinetId:this.planCabinetId}).then(response=>{
          this.$modal.msgSuccess("修改成功");
          this.closeBatchPlan();
          this.cancelPlan();
          this.getList();
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除库位编号为"' + row.name + '"的数据项？').then(() => {
          this.loading = true;
          return delLocator(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).finally(() => {
          this.loading = false;
        });
      },
      handleDrawShow(item){
        let items = [];
        if(item.propertyVos && item.propertyVos.length > 0){
          this.locatorName = (item.areaName?item.areaName:'')
          +(item.groupName?item.groupName:'')
          +(item.cabinetName?item.cabinetName:'')
          +(item.name?item.name:'')
          +'-物品清单';
          this.locatorProperties = item.propertyVos;
          this.locatorDrawer = true;
        }
      },
      handleClose(){
        this.locatorName = '';
        this.locatorDrawer =false;
        this.locatorProperties = [];
      },
      getSummaries(param) {
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            sums[index] = '合计';
            return;
          }
          if (index < 5) {
            sums[index] = '';
            return;
          }

          const values = data.map(item => Number(item[column.property]));
          console.info('values===>',values)
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              console.info('value===>',value)
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
          }
        });

        return sums;
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('wms/locator/export', {
          ...this.queryParams
        }, `locator_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
